package mobileann.mafamily.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.mofind.android.base.L;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.net.Uri;

public class ScaleCompressUtil {
	public static Bitmap compressImageFromFile(String srcPath, float ww,
			float hh, int size) {
		BitmapFactory.Options newOpts = new BitmapFactory.Options();
		newOpts.inJustDecodeBounds = true;
		Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
		newOpts.inJustDecodeBounds = false;
		int w = newOpts.outWidth;
		int h = newOpts.outHeight;
		int be = 1;
		if (w > h && w > ww) {
			be = (int) (newOpts.outWidth / ww);
		} else if (w < h && h > hh) {
			be = (int) (newOpts.outHeight / hh);
		}
		if (be <= 0)
			be = 1;
		newOpts.inSampleSize = be;
		newOpts.inPreferredConfig = Config.RGB_565;
		bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
		return QualityCompressUtil.compressImage(bitmap, size);
	}

	public static ByteArrayOutputStream compressImageFromFile2os(
			String srcPath, float ww, float hh, int size) {
		BitmapFactory.Options newOpts = new BitmapFactory.Options();
		newOpts.inJustDecodeBounds = true;
		Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
		newOpts.inJustDecodeBounds = false;
		int w = newOpts.outWidth;
		int h = newOpts.outHeight;
		int be = 1;
		if (w > h && w > ww) {
			be = (int) (newOpts.outWidth / ww);
		} else if (w < h && h > hh) {
			be = (int) (newOpts.outHeight / hh);
		}
		if (be <= 0)
			be = 1;
		newOpts.inSampleSize = be;
		newOpts.inPreferredConfig = Config.RGB_565;		
		bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
		return QualityCompressUtil.compressImage2os(bitmap, size);
	}

	public static Bitmap compressImageFromUri(Context context, Uri uri,
			float ww, float hh, int size) {
		BitmapFactory.Options factory = new BitmapFactory.Options();
		factory.inJustDecodeBounds = true;
		Bitmap bmp;
		try {
			bmp = BitmapFactory.decodeStream(context.getContentResolver()
					.openInputStream(uri), null, factory);
			int hRatio = (int) Math.ceil(factory.outHeight / (float) hh);
			int wRatio = (int) Math.ceil(factory.outWidth / (float) ww);
			System.out.println("hRatio:" + hRatio + "  wRatio:" + wRatio);
			if (hRatio > 1 || wRatio > 1) {
				if (hRatio > wRatio) {
					factory.inSampleSize = hRatio;
				} else
					factory.inSampleSize = wRatio;
			}
			factory.inJustDecodeBounds = false;			
			bmp = BitmapFactory.decodeStream(context.getContentResolver()
					.openInputStream(uri), null, factory);
			return QualityCompressUtil.compressImage(bmp, size);
		} catch (FileNotFoundException e) {
			L.e("maf", "error--", e);
		}
		return null;
	}

	public static ByteArrayOutputStream compressImageFromUri2os(
			Context context, Uri uri, float ww, float hh, int size) {
		BitmapFactory.Options factory = new BitmapFactory.Options();
		factory.inJustDecodeBounds = true;
		Bitmap bmp;
		try {
			bmp = BitmapFactory.decodeStream(context.getContentResolver()
					.openInputStream(uri), null, factory);
			int hRatio = (int) Math.ceil(factory.outHeight / (float) hh);
			int wRatio = (int) Math.ceil(factory.outWidth / (float) ww);
			System.out.println("hRatio:" + hRatio + "  wRatio:" + wRatio);
			if (hRatio > 1 || wRatio > 1) {
				if (hRatio > wRatio) {
					factory.inSampleSize = hRatio;
				} else
					factory.inSampleSize = wRatio;
			}
			factory.inJustDecodeBounds = false;			
			bmp = BitmapFactory.decodeStream(context.getContentResolver()
					.openInputStream(uri), null, factory);
			return QualityCompressUtil.compressImage2os(bmp, size);
		} catch (FileNotFoundException e) {
			L.e("maf", "error--", e);
		}
		return null;
	}

	public static Bitmap compressImageFromUri(Context context, Uri uri,
			int ratio, int size) {
		BitmapFactory.Options factory = new BitmapFactory.Options();
		Bitmap bmp;
		try {
			factory.inSampleSize = ratio;
			factory.inJustDecodeBounds = false;
			factory.inPreferredConfig = Config.RGB_565;
			bmp = BitmapFactory.decodeStream(context.getContentResolver()
					.openInputStream(uri), null, factory);
			return QualityCompressUtil.compressImage(bmp, size);
		} catch (FileNotFoundException e) {
			L.e("maf", "error--", e);
		}
		return null;
	}

	public static ByteArrayOutputStream compressImageFromUri2os(
			Context context, Uri uri, int ratio, int size) {
		BitmapFactory.Options factory = new BitmapFactory.Options();
		Bitmap bmp;
		try {
			factory.inSampleSize = ratio;
			factory.inJustDecodeBounds = false;
			factory.inPreferredConfig = Config.RGB_565;
			bmp = BitmapFactory.decodeStream(context.getContentResolver()
					.openInputStream(uri), null, factory);
			return QualityCompressUtil.compressImage2os(bmp, size);
		} catch (FileNotFoundException e) {
			L.e("maf", "error--", e);
		}
		return null;
	}

	public static void saveBitmap(Bitmap bm, String filePath) {
		File f = new File(filePath);
		if (f.exists()) {
			f.delete();
		}
		try {
			FileOutputStream out = new FileOutputStream(f);
			bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
			out.flush();
			out.close();
		} catch (FileNotFoundException e) {
			L.e("maf", "error--", e);
		} catch (IOException e) {
			L.e("maf", "error--", e);
		}
		bm.recycle();
		bm = null;
	}

	public static void saveBitmap(ByteArrayOutputStream bos, String filePath) {
		File f = new File(filePath);

		if (f.exists()) {
			f.delete();
		}

		try {
			f.createNewFile();
			FileOutputStream out = new FileOutputStream(f);
			out.write(bos.toByteArray());
			out.flush();
			out.close();
		} catch (FileNotFoundException e) {
			L.e("maf", "error--", e);
		} catch (IOException e) {
			L.e("maf", "error--", e);
		}
		try {
			bos.close();
		} catch (IOException e) {
			L.e("maf", "error--", e);
		}
	}
}
